package com.atguigu.day08;

import com.atguigu.bean.WaterSensor;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.DataTypes;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.TableResult;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.descriptors.Json;
import org.apache.flink.table.descriptors.Kafka;
import org.apache.flink.table.descriptors.Schema;
import org.apache.kafka.clients.producer.ProducerConfig;

import static org.apache.flink.table.api.Expressions.$;

public class Flink09_SQL_Demo {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        DataStreamSource<WaterSensor> waterSensorStream =
                env.fromElements(new WaterSensor("sensor_1", 1000L, 10),
                        new WaterSensor("sensor_1", 2000L, 20),
                        new WaterSensor("sensor_2", 3000L, 30),
                        new WaterSensor("sensor_1", 4000L, 40),
                        new WaterSensor("sensor_1", 5000L, 50),
                        new WaterSensor("sensor_2", 6000L, 60));


        //2.获取表的执行环境
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        //将流转为表（未注册的表）
        Table tableSource = tableEnv.fromDataStream(waterSensorStream);

        //TODO 将流转为表（已注册的表）
//        tableEnv.createTemporaryView("sensor", waterSensorStream);

        //通过Table对象注册表
        tableEnv.createTemporaryView("sensor", tableSource);

        //3.查询未注册的表
//        TableResult tableResult = tableEnv.executeSql("select id,sum(vc) vcSum from " + tableSource + " group by id");
//        tableResult.print();

        //打印表中的数据 方式二：
//        Table table = tableEnv.sqlQuery("select id,sum(vc) vcSum from " + tableSource + " group by id");
//        table.execute().print();

        //查询已注册的表
        tableEnv.executeSql("select id,sum(vc) from sensor group by id").print();

    }
}
